// Copyright (C) 2006 Taylor L. Riché <riche@cs.utexas.edu>
//  
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software 
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//

#ifndef UDP_PACKET_HH
#define UDP_PACKET_HH

#include <netinet/udp.h>

#include "IP_Packet.hh"

namespace pktTypes {

  const unsigned int UDP_HSIZE = 0x8; // 8

  class UDP_Packet : public IP_Packet
  {
  protected:
    udphdr * udpHdr;
    payload_t udp_payload;
    uint16_t udpLength;
  public:
    UDP_Packet() : udpHdr(NULL), udp_payload(NULL) {}
    UDP_Packet(const IP_Packet & ipPkt) : IP_Packet(ipPkt)
    {
      assert(payload != NULL);
      udpHdr = (udphdr *)ip_payload;
      udp_payload = ((payload_t)udpHdr) + UDP_HSIZE;
      udpLength = length - ETH_HSIZE - IPv4_HSIZE;
    }
    virtual ~UDP_Packet() {}
    
    inline void setSrcPort(uint16_t sp) {udpHdr->source = htons(sp);}
    inline uint16_t getSrcPort() const {return ntohs(udpHdr->source);}
    inline void setDstPort(uint16_t dp) {udpHdr->dest = htons(dp);}
    inline uint16_t getDstPort() const {return ntohs(udpHdr->dest);}
    inline uint16_t getUDPLength() const {return ntohs(udpHdr->len);}
    inline void setUDPLength(uint16_t l) {udpHdr->len = htons(l);}
    payload_t getPayload() const {return udp_payload;}
    virtual int chksum();
    inline void setChkSum(uint16_t c) {udpHdr->check = ntohs(c);}
  };



}

#endif // UDP_PACKET_HH
